$(function(){
    var count = 10;// 默认一次检索10条记录
    new Vue({
        el:"#app",
        data:{
            grades: [],
            students:[],
            currentStuIndex: 0,
            currentGrade: {},
            searchKey: '',
            currentPage: 1,
            pageNum:0
        },
        methods:{
            changeGrade(grade) {
                this.currentGrade = grade;
                this.pageNum = (grade.headcount % count) == 0?(grade.headcount / count):((Math.floor(grade.headcount / count) + 1));
                console.log("pageNum-->"+this.pageNum);
                this.pageSelect(grade.gradeId, 1, this.condition);
            },
            changeCurrentPage(pno) {
                console.log('当前页号-->' + pno);
                this.currentPage = pno;
                this.pageSelect(this.currentGrade.gradeId, pno,this.condition);
            },
            search() {
                console.log(this.searchKey);
                this.pageSelect(this.currentGrade.gradeId, 1, this.searchKey);
                this.pageNum = (this.students.length % count) == 0?(this.students.length / count):((Math.floor(this.students.length / count) + 1));
            },
            pageSelect(currentGradeId,currentPage,searchKey) {
                this.$http.get('user/pageSelect',
                    {params:{count:count,currentGradeId:currentGradeId,currentPage:currentPage,searchKey:searchKey}}).then(function(res){
                    console.log(res.body);
                    let result = res.body.data;
                    this.students = result.students;
                    this.currentPage = currentPage;
                    console.log("students size-->"+this.students.length);
                },function(){
                    console.log('请求失败！');
                });
            },
            delOrUpdate(tag) {
                if (tag == 0) {
                    console.log('del');
                    // 置空学号作为删除还是修改的标识
                    this.students[this.currentStuIndex].stuId = null;
                } else {
                    console.log('update');
                }
                this.$http.post('user/delOrUpdate',
                    {id:this.students[this.currentStuIndex].id,department:this.students[this.currentStuIndex].department,
                        gradeId:this.students[this.currentStuIndex].gradeId,
                        inClass:this.students[this.currentStuIndex].inClass,stuId:this.students[this.currentStuIndex].stuId,
                        name:this.students[this.currentStuIndex].name,phone:this.students[this.currentStuIndex].phone,},{emulateJSON:true}).then(function (res) {
                    console.log(res.body);
                    if (res.body.success) {
                        window.location.href="backStudentInfo.action";
                    } else {
                    }
                },function(){
                    console.log('请求失败处理');
                });
            },
        },
        created() {
            this.$http.get('user/pageSelect',
                {params:{currentPage:this.currentPage}}).then(function(res){
                console.log(res.body);
                let result = res.body.data;
                this.grades = result.grades;
                this.students = result.students;
                this.pageNum = (this.grades[0].headcount % count) == 0?(this.grades[0].headcount / count):((Math.floor(this.grades[0].headcount / count) + 1));
                this.currentGrade = result.grades[0];
            },function(){
                console.log('请求失败！');
            });
        }
    });
});